﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Gift
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GiftItems : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int page = 1;
            int giftClass = 9999;
            List<int?> count;
            if (context.Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(context.Request.QueryString["page"]);
            }
            if (context.Request.QueryString["giftclass"] != null)
            {
                giftClass = Convert.ToInt32(context.Request.QueryString["giftclass"]);
            }
            context.Response.ContentType = "html/plain";
            context.Response.Write(GiftPhotoList(page, giftClass, out count, context));
        }
        /// <summary>
        /// 礼物列表
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="giftClass">分类</param>
        /// <param name="count">返回页码</param>
        /// <returns></returns>
        private string GiftPhotoList(int page, int giftClass, out List<int?> count,HttpContext context)
        {
            StringBuilder sbGiftImg = new StringBuilder();
            string strGiftImg = context.Cache["user_giftphoto_li"].ToString();
            string where = string.Empty;
            string fun = "fillhtml('giftitems','/Gift/GiftItems.ashx?page=$num$')";
            if (giftClass != 9999)
            {
                where = string.Format("FGiftClass = {0}", giftClass);
                fun = string.Format("fillhtml('giftitems','/Gift/GiftItems.ashx?page=$num$'&giftclass={0})", giftClass);
            }
            DataTable dtGiftImg = new Jy123.BLL.User.Gift().GiftList(page, 24, where, out count).Tables[0];
            for (int i = 0; i < dtGiftImg.Rows.Count; i++)
            {
                string str = strGiftImg;
                str = str.Replace("$Picture$", dtGiftImg.Rows[i]["Picture"].ToString())
                    .Replace("$GiftID$", dtGiftImg.Rows[i]["ID"].ToString())
                    .Replace("$GName$", dtGiftImg.Rows[i]["GName"].ToString());
                sbGiftImg.Append(str);
            }
            //加入分页按钮的html
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                  {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            //如果记录大于1页
            if (count[1] > 1)
            {
                MoonPage mp = new MoonPage(true, false);
                mp.Count = Convert.ToInt32(count[1]);
                sbGiftImg.AppendFormat(pageHtml, mp.GetStrPage(page, "no", "sel", fun), 1 + (page - 1) * 24, ((page * 24) > count[0]) ? count[0] : page * 24, count[0]);
            }
            sbGiftImg.Append(@"<script language=""javascript"" type=""text/javascript"">$(function(){bindGiftItems();})</script>");
            return sbGiftImg.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
